//
//  TKYQuestionLevelController.m
//  StationHouseHelper
//
//  Created by apple on 2018/7/27.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYQuestionLevelController.h"
#import "TKYQuestionLevelModel.h"
@interface TKYQuestionLevelController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)BaseTabelView *tableView;
@property (nonatomic, strong)NSMutableArray <TKYQuestionLevelModel *>*questionLevelS;

@property(nonatomic,strong)NSIndexPath *lastPath;

/**
 选中的问题级别
 */
@property (nonatomic, strong)TKYQuestionLevelModel * currentQuestionLevel;

@end

@implementation TKYQuestionLevelController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加监理单位列表
    [self.view addSubview:self.tableView];
    //设置导航
    [self setupNavigation];
    //请求数
    [self requestQuestionLevelData];
}

#pragma mark-- 请求数据
- (void)requestQuestionLevelData{
    NSString * url = [NSString stringWithFormat:@"%@%@token=%@&pid=2c999ad5628569460162856946900000",kApiBaseUrl, KGetqualitySafeLevelUrl,[[NSUserDefaults standardUserDefaults]objectForKey:kToken]];
    [SVProgressHUD showWithStatus:@"正在加载数据"];
    
    [KHttpRequestTool GET:url parameters:nil success:^(id response) {
        if ([response[@"code"] integerValue] == 0) {
            self.questionLevelS = [TKYQuestionLevelModel  mj_objectArrayWithKeyValuesArray:response[@"dictList"]];
            [SVProgressHUD dismiss];
        }else{
            [SVProgressHUD showErrorWithStatus:response[@"msg"]];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误, 请稍候再试"];

    }];
    
}

/**
 设置导航
 */
- (void)setupNavigation{
    self.navigationItem.title = @"问题级别";
    //设置导航栏文字颜色
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.barTintColor = KNavColor;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(saveButtonClick:)];
}

- (void)saveButtonClick:(UIBarButtonItem *)barButtonItem{
    self.selectedQuestionLevelBlock(self.currentQuestionLevel);
    [self.navigationController popViewControllerAnimated:YES];
    
}
#pragma  mark --- UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.questionLevelS.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    cell.textLabel.text = self.questionLevelS[indexPath.row].name;
    NSInteger row = [indexPath row];
    NSInteger oldRow = [self.lastPath row];
    if (row == oldRow && self.lastPath!=nil) {
        //这个是系统中对勾的那种选择框
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //这里最好不要声明int类型的，个人建议
    NSInteger newRow = [indexPath row];
    NSInteger oldRow = (self.lastPath !=nil)?[self.lastPath row]:-1;
    if (newRow != oldRow) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.currentQuestionLevel = self.questionLevelS[indexPath.row];
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        self .lastPath = indexPath;
    }
}

#pragma mark --- 懒加载
- (BaseTabelView *)tableView{
    if (_tableView == nil) {
        _tableView = [[BaseTabelView alloc]initWithFrame:CGRectMake(0, MyNavBarHeight, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}

- (NSMutableArray<TKYQuestionLevelModel *> *)questionLevelS{
    if (_questionLevelS == nil) {
        _questionLevelS = [NSMutableArray array];
    }
    return _questionLevelS;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
